package com.lyc.security;

import com.lyc.utils.MD5Util;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.PasswordMatcher;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

/**
 * @Author YooLin
 * @Date 2017/8/14 19:40
 * @Description 自定义密码校验器
 */
public class CustomerPwdMatcher extends SimpleCredentialsMatcher {
    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        if (token instanceof UsernamePasswordToken){
            UsernamePasswordToken passwordToken = (UsernamePasswordToken) token;
            String input_password = String.valueOf(passwordToken.getPassword());
            //获取身份认证保存在AuthenticationInfo中的密码（数据库中）
            String db_password = getCredentials(info).toString();
            try {
                boolean isPwd = MD5Util.checkPassword(input_password,passwordToken.getUsername(), db_password);
                return isPwd;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}
